import pygame, sys
from random import randint
import random
import g
from screenOMatic9000 import screen
from faction import faction
from map import map

random.seed()

pygame.init()
scr = screen()
g.scr = scr
clock = pygame.time.Clock()

def input_handler():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
            
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                sys.exit()
                
            if event.key == pygame.K_UP:
                pass
            if event.key == pygame.K_DOWN:
                pass
            if event.key == pygame.K_LEFT:
                pass
            if event.key == pygame.K_RIGHT:
                pass

def logic_handler():
    for f in factions:
        for d in f.dudes:
            d.move()
    g.map.colorshift()

def gfx_handler():
    scr.screen.fill((0,0,0))
    for r in g.map.geoms:
        scr.drawrect(r.x,r.y,r.w,r.h, g.map.color)
    for f in factions:
        for d in f.dudes:
            scr.drawrect(d.rect.x, d.rect.y, d.rect.w, d.rect.h, d.color)
    scr.flip()

def game_loop():
    while(True):
        input_handler()
        logic_handler()
        gfx_handler()
        clock.tick(60)

def init_population():
    for i in range(3):
        factions.append(faction((randint(0,255),randint(0,255),randint(0,255))))

    for f in factions:
        for i in range(10):
            f.add_npc()

factions = []
g.factions = factions
g.map = map()
g.map.generate()

init_population()
game_loop()
